
# coding:utf-8

from system.skill import SKILL_LST
from utils.suger import *


class UnitSkill(object):
    def __init__(self, unit):
        #自身拥有的技能，key为id，value为熟练度
        self.skl = {}
        self.skl_cd = {}
        self.base = unit

    def __getitem__(self, key):
        if key in self.skl:
            return self.skl[key]

    def __iter__(self):
        return self.skl.iteritems()

    def check(self, id):
        '''法术可用性检查。'''
        if id in self.skl_cd:
            return 1
        else:
            skl = SKILL_LST[id]
            use_time = self.skl[id]
            level = skl.get_level(use_time)
            return skl.check(self.base, level)

    def cast(self, id):
        if id in self.skl:
            skl = SKILL_LST[id]
            use_time = self.skl[id]
            level = skl.get_level(use_time)
            skl.cast(self.base, level)
            self.skl[id] += 1

            def OnFire():
                self.skl_cd[id] = None
                del self.skl_cd[id]
            t = flux.Timeout(skl.cd)
            t.OnFire = OnFire
            t.Start()
            self.skl_cd[id] = t

    def learn(self, id, times=0):
        ''' 学习技能，times是熟练度。也可用于提升熟练度。 '''
        if not id in self.skl:
            self.skl[id] = times
        else:
            self.skl[id] += times
